package com.itheima.mm.wx.service.impl;

import com.itheima.mm.common.QuestionConst;
import com.itheima.mm.dto.WxCategoryDTO;
import com.itheima.mm.entity.Result;
import com.itheima.mm.vo.wx.WxCategoryVO;
import com.itheima.mm.wx.mapper.CatalogMapper;
import com.itheima.mm.wx.mapper.CompanyMapper;
import com.itheima.mm.wx.mapper.IndustryMapper;
import com.itheima.mm.wx.mapper.QuestionMapper;
import com.itheima.mm.wx.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description CategoryServiceImpl
 * @Author Yc
 * @Date 2024-06-27
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CatalogMapper catalogMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private IndustryMapper industryMapper;

    /**
     * 获取首页题库分类列表
     * @param wxCategoryDTO
     * @param memberId
     * @param courseId
     * @param cityId
     * @return
     */
    @Override
    public List<WxCategoryVO> findCategory(
            WxCategoryDTO wxCategoryDTO, Integer memberId, Integer courseId, Integer cityId) {
        Integer categoryKind = wxCategoryDTO.getCategoryKind();
        List<WxCategoryVO> categoryList = null;
        if(categoryKind==QuestionConst.CategoryKind.CATALOG.getId()){
            categoryList = catalogMapper.selectCatalogListByQueryParam(memberId, courseId);
        }else if (categoryKind==QuestionConst.CategoryKind.COMPANY.getId()){
            categoryList = companyMapper.selectCompanyListByQueryParam(memberId,courseId,cityId);
        }else if (categoryKind==QuestionConst.CategoryKind.INDUSTRY.getId()){
            categoryList = industryMapper.selectIndustryListByQueryParam();
        }else {
            log.warn("未知的categoryList:{}",categoryList);
            return new ArrayList<>();
        }
        return categoryList;
    }

    @Override
    public Map findCategoryQuestion(Map<String, Object> queryMap) {
        Integer categoryKind = (Integer) queryMap.get("categoryKind");
        log.info("Querying categoryKind: {}", categoryKind);
        Map map = null;
        if(categoryKind==QuestionConst.CategoryKind.CATALOG.getId()){
            queryMap.put("status", QuestionConst.CatalogStatus.ENABLE.ordinal());
            List<Map> categoryList = catalogMapper.selectCatalogListByQueryParam2(queryMap);
            log.info("categoryList: {}", categoryList);
            if(categoryList!=null&&!categoryList.isEmpty()){
                map = categoryList.get(0);
                List<Map> questionList = questionMapper.selectQuestionListByQueryParam(queryMap);
                map.put("items",questionList);
            }
            return map;
        } else if (categoryKind==QuestionConst.CategoryKind.COMPANY.getId()) {
            List<Map> categoryList = companyMapper.selectCompanyListByQueryParam2(queryMap);
            if(categoryList!=null&&!categoryList.isEmpty()){
                map = categoryList.get(0);
                List<Map> questionList = questionMapper.selectQuestionListByQueryParam(queryMap);
                map.put("items",questionList);
            }
            return map;
        } else if (categoryKind==QuestionConst.CategoryKind.INDUSTRY.getId()) {
            List<Map> categoryList = industryMapper.selectIndustryListByQueryParam2(queryMap);
            if(categoryList!=null&&!categoryList.isEmpty()){
                map = categoryList.get(0);
                List<Map> questionList = questionMapper.selectQuestionListByQueryParam(queryMap);
                map.put("items",questionList);
            }
            return map;
        }else {
            return new HashMap();
        }
    }
}
